/**
*
*
*
<script type="text/javascript" src="js/jquery.horizontalSlider.js"></script>

<script type="text/javascript">
	$(document).ready(function(){
		$('#client_slider').horizontalSlider();
		//or $('#client_slider').horizontalSlider({direction:'left'});
	});
</script>

<div id="client_slider">
	<img src="i/logo_sberbank.png">
	<img src="i/logo_mtsbank.png">
	<img src="i/logo_vtb24bank.png">
	<img src="i/logo_russtandart.png">
</div>

*
*/
(function($){
	$.fn.horizontalSlider = function(options){
		
		var op = $.extend({
				
				block_width: 190,	/* ширина каждого блока */
				block_margin_right: 10,	/* правое поле каждого блока */
				fit_images: true,	/* поджимаем ли картинки под размер блока */
				
				direction: 'right',	/* направление */
				duration: 3000,	/* длительность */
				interval:10000	/* интервал между движением */
				
			}, options);
		
		
		return this.each(function(){
			var pFix = this.id+'_horizontalSlide_',// префикс
				blockClass = pFix+'block',
				wrapperClass = pFix+'block_wrapper',
			
				slider_height= this.offsetHeight || 80,
				slider_width= this.offsetWidth || 800;
			
			$(this).css({
				height:	slider_height+'px',
				width:	slider_width+'px',
				overflow: 'hidden',
				position: 'relative'
			});
			
			var kids = $(this).children();
			kids.wrap('<div class="'+blockClass+'"></div>');
			$('.'+blockClass).css({
				display: 'block',
				float:'left',
				overflow: 'hidden',
				'margin-right': op.block_margin_right+'px',
				width: op.block_width+'px'
			});
			if( op.fit_images )$('.'+blockClass+' img').css({width: op.block_width+'px'});
			
			$(this).wrapInner('<div class="'+wrapperClass+'"></div>');
			$('.'+wrapperClass).css({ position: 'absolute', left:'0px' });
			
			var block_wrapper = $('.'+wrapperClass),
				slide_length = op.block_width + op.block_margin_right,
				sumW = block_wrapper.children().length*( slide_length );
			block_wrapper.css( 'width', sumW+'px' );
			if( sumW > slider_width+op.block_margin_right ) setInterval( slide, op.interval);
			
			
			function slide(){
				var inc_str = '-=';/* move to left */
				
				if(op.direction=='right'){
					var childs = block_wrapper.children(),
						last = childs.eq(childs.length-1); 
					block_wrapper.prepend(last).css('left','-='+slide_length+'px');
					inc_str = '+='; /* move to right */
				} 
				
				block_wrapper.animate(
					{
						'left': inc_str+slide_length+'px'
					}, {
						duration: op.duration,
						complete: function() {
							if(op.direction!='right'){
								var first = block_wrapper.children().eq(0); 
								block_wrapper.append(first).css('left','+='+slide_length+'px');
							}
						}
					}
				);
			};	/* function slide() */
			
		});/* return this.each(function() */

	};
}(jQuery));